﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using HF.Framework.DataClientDBAgent;
using HF.WorkFlow.DataContract;
using HF.Framework.SqlDataContract;
namespace HF.Forms.Data
{
    /// <summary>
    /// 数据访问类MainChildForm。
    /// </summary>
    public class MainChildForm
    {
        #region  私有字段
        private SqlDataItem sqlDataItem = new SqlDataItem();
        private string[] fieldList = { "MainChildId", "ChildFormId", "MainFormId", "FormOrder", "FormState" };
        private string tableName = "HF_MainChildForm";
        private string keyField = "MainChildId";
        private string sqlString = "";

        #endregion

        #region  私有方法
        private void setParameter(MainChildFormEntity entity)
        {
            sqlDataItem.ParameterList.Clear();
            sqlDataItem.AppendParameter("@MainChildId", entity.MainChildId, typeof(string));
            sqlDataItem.AppendParameter("@ChildFormId", entity.ChildFormId, typeof(string));
            sqlDataItem.AppendParameter("@MainFormId", entity.MainFormId, typeof(string));
            sqlDataItem.AppendParameter("@FormOrder", entity.FormOrder, typeof(int));
            sqlDataItem.AppendParameter("@FormState", entity.FormState, typeof(string));
        }

        private void setInsertSql()
        {
            string tmpValueList = "";
            string tmpFieldName = "";
            sqlString = "insert into " + tableName + "(";
            int tmpInt = this.fieldList.Length;
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + ", ";
                tmpValueList = tmpValueList + "@" + tmpFieldName + ",";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName;
            tmpValueList = tmpValueList + "@" + tmpFieldName;
            this.sqlString = sqlString + ")values(" + tmpValueList + ")";
            sqlDataItem.CommandText = sqlString;
        }

        private void setUpdateSql()
        {
            string tmpFieldName = "";
            int tmpInt = this.fieldList.Length;
            sqlString = "update " + tableName + " set ";
            for (int i = 0; i < tmpInt - 1; i++)
            {
                tmpFieldName = fieldList[i].ToString();
                sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName + ", ";
            }
            tmpFieldName = this.fieldList[tmpInt - 1].ToString();
            sqlString = sqlString + tmpFieldName + " = @" + tmpFieldName;
            sqlString = sqlString + " where " + keyField + "=@" + this.keyField;
            sqlDataItem.CommandText = sqlString;
        }

        public MainChildFormEntity GetMainChildFormEntity(DataRow dr)
        {
            MainChildFormEntity entity = new MainChildFormEntity();
            string value = "";
            entity.MainChildId = dr["MainChildId"].ToString();
            entity.ChildFormId = dr["ChildFormId"].ToString();
            entity.MainFormId = dr["MainFormId"].ToString();
            value = dr["FormOrder"].ToString();
            if (string.IsNullOrEmpty(value) == false)
            {
                entity.FormOrder = Convert.ToInt32(value);
            }
            entity.FormState = dr["FormState"].ToString();
            return entity;
        }

        #endregion

        #region  构造函数
        public MainChildForm()
        { }
        public static MainChildForm GetInstance()
        {
            return new MainChildForm();
        }
        #endregion

        #region  公有方法
        #region  基本方法
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Insert(MainChildFormEntity entity)
        {
            try
            {
                setInsertSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="entity">数据实体</param>
        /// <returns>影响的记录条数</returns>
        public int Update(MainChildFormEntity entity)
        {
            try
            {
                setUpdateSql();//设定insert语句
                setParameter(entity);//设定参数
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteNonQuery(sqlDataItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string mainChildId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            SqlDataItem sqlItem = new SqlDataItem();
            sqlItem.CommandText = tmpSql;
            sqlItem.AppendParameter("@" + keyField, mainChildId);
            ClientDBAgent agent = new ClientDBAgent();
            return agent.RecordExists(sqlItem);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="mainChildId">主键Id</param>
        /// <returns>影响的记录条数</returns>
        public void Delete(string mainChildId)
        {
            try
            {
                string tmpSql = "delete from " + tableName + " where  " + keyField + "=@" + keyField;
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, mainChildId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一条数据记录
        /// </summary>
        /// <param name="MainChildId">主键Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetMainChildFormTable(string mainChildId)
        {
            string tmpSql = "select * from " + tableName + " where " + keyField + "=@" + keyField;
            try
            {
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@" + keyField, mainChildId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取一个数据实体
        /// </summary>
        /// <param name="mainChildId">主键Id</param>
        /// <returns>DataTable</returns>
        public MainChildFormEntity GetMainChildFormEntity(string mainChildId)
        {
            DataTable dt = GetMainChildFormTable(mainChildId);
            if (dt != null && dt.Rows.Count > 0)
            {
                return GetMainChildFormEntity(dt.Rows[0]);
            }
            return null;
        }

        #endregion

        #region  扩展方法
        //此处添加扩展方法

        /// <summary>
        /// 把子表单移动到主表单中
        /// </summary>
        /// <param name="mainFormId">主表单id</param>
        /// <param name="childFormId">子表单id</param>
        /// <param name="formOrder">顺序号,以此排序</param>
        /// <param name="formState">表单读写状态</param>
        /// <returns>是否成功</returns>
        public  bool MoveChildToMain(string mainFormId, string childFormId, int formOrder, string formState)
        {
            MainChildFormEntity entity = new MainChildFormEntity();
            try
            {
                entity.MainChildId = Guid.NewGuid().ToString();
                entity.MainFormId = mainFormId;
                entity.ChildFormId = childFormId;
                entity.FormOrder = formOrder;
                entity.FormState = formState;
                MainChildForm.GetInstance().Insert(entity);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 把子表单从主表单中移除
        /// </summary>
        /// <param name="mainFormId">主表单id</param>
        /// <param name="childFormId">子表单id</param>
        /// <returns>是否成功</returns>
        public  bool RemoveChildFromMain(string mainFormId, string childFormId)
        {
            try
            {
                string tmpSql = "delete HF_MainChildForm where mainFormId=@mainFormId and childFormId=@childFormId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@mainFormId", mainFormId);
                sqlItem.AppendParameter("@childFormId", childFormId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
                return true;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 移除主表单中所有子表单
        /// </summary>
        /// <param name="mainUserCtrlId">主表单id</param>
        ///   <returns>是否成功</returns>
        public bool RemoveAllChildFromMain(string mainFormId)
        {
            try
            {
                string tmpSql = "delete HF_MainChildForm where mainFormId=@mainFormId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpSql;
                sqlItem.AppendParameter("@mainFormId", mainFormId);
                ClientDBAgent agent = new ClientDBAgent();
                agent.ExecuteNonQuery(sqlItem);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得流程的所有表单配置关系信息
        /// </summary>
        /// <param name="workFlowId"></param>
        /// <returns></returns>
        public DataTable GetMainChildFormsOfWorkFlow(string workFlowId)
        {
            try
            {
                string tmpStr = "select distinct wcf.* from HF_WorkTaskForm wtf left join HF_MainChildForm wcf on wtf.MainFormId=wcf.MainFormId "+
                                     "   where wtf.workflowid=@workFlowId";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workFlowId", workFlowId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得主表单与子表单的视图信息
        /// </summary>
        /// <param name="mainFormId"></param>
        /// <returns>DataTable</returns>
        public DataTable GetMainChildFormView(string mainFormId)
        {
            try
            {
                string tmpStr = @"SELECT * from HF_MainChildFormsView
                                   where MainFormId=@mainFormId order by FormOrder ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@mainFormId", mainFormId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得主表单与子表单的视图信息
        /// </summary>
        /// <param name="mainFormId"></param>
        /// <returns>Entitys</returns>
        public List<MainChildFormViewEntity>  GetMainChildFormViewEntitys(string mainFormId)
        {
            List<MainChildFormViewEntity> result = new List<MainChildFormViewEntity>();
            DataTable dt = GetMainChildFormView(mainFormId);
            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    MainChildFormViewEntity entity = new MainChildFormViewEntity();
                    entity.MainChildId = dr["MainChildId"].ToString();
                    entity.MainFormId = dr["MainFormId"].ToString();
                    entity.ChildFormId = dr["ChildFormId"].ToString();
                    entity.MainCaption = dr["MainCaption"].ToString();
                    entity.ChildCaption = dr["ChildCaption"].ToString();
                    entity.FileName = dr["FileName"].ToString();
                    entity.FilePath= dr["FilePath"].ToString();
                    entity.FormOrder =Convert.ToInt16(dr["FormOrder"]);
                    entity.FormState = dr["FormState"].ToString();
                    entity.Description = dr["Description"].ToString();
                    result.Add(entity);
                }
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 获得任务节点与表单的视图信息
        /// </summary>
        /// <param name="workTaskId">任务模板Id</param>
        /// <returns>DataTable</returns>
        public DataTable GetWorkTaskFormsView(string workTaskId)
        {
            try
            {
                string tmpStr = @"SELECT * from HF_WorkTaskFormsView
                                   where workTaskId=@workTaskId order by FormOrder ";
                SqlDataItem sqlItem = new SqlDataItem();
                sqlItem.CommandText = tmpStr;
                sqlItem.AppendParameter("@workTaskId", workTaskId);
                ClientDBAgent agent = new ClientDBAgent();
                return agent.ExecuteDataTable(sqlItem);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
        #endregion
    }
}

